import { Component, OnInit, Input, ViewChild} from '@angular/core';
import { zh_CN } from 'ng-zorro-antd-mobile';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { ModalController } from '@ionic/angular';
import { EventService } from 'src/app/shared/service/event.service';
import { HttpService } from 'src/app/shared/service/http.service';

declare var $: any;
@Component({
  selector: 'app-order-time-filter',
  templateUrl: './order-time-filter.component.html',
  styleUrls: ['./order-time-filter.component.scss'],
})
export class OrderTimeFilterComponent implements OnInit {
  @Input() tagData: any;
  locale = zh_CN;
  serviceStartTime: any;
  minDate: any;
  stratTitle: any; // 当前的开始时间
  flag = true;
  timeContent: any = '';
  shopContent: any = '';
  startDate: any = '';  // 已选择的开始时间
  endDate: any = '';    // 已选择的结束时间
  constructor(
    public appGlobalService: AppGlobalService,
    public modalCtrl: ModalController,
    public eventService: EventService,
    public httpService: HttpService
  ) { }
  ngOnInit() {
    if (this.tagData.filterItems && this.tagData.filterItems[0]) {
      this.tagData.filterItems[0].subFilterItem.forEach(e => {
      if (this.serviceStartTime === undefined) {
        this.serviceStartTime = e.startDate;
      }
      e.endDate = this.appGlobalService.getNowFormatDateYYYYMMDD();
      e.startDate = this.appGlobalService.getNowFormatDateYYYYMMDD2(this.serviceStartTime);
      });
    }
    if (this.eventService.eventEmit.listenerCount('emitFilterTime') > 0) {
      this.eventService.eventEmit.removeListener('emitFilterTime');
    }
    this.eventService.eventEmit.on("emitFilterTime", res => {

      if (this.shopContent === '' && this.timeContent === '') {
        if (this.endDate === '' && this.startDate === '') {
          this.appGlobalService.showToast('至少选择一项~', 1500, "top");
          return;
        } else if (this.endDate === '' || this.startDate === '') {
          this.appGlobalService.showToast('请选择完整哦~', 1500, "top");
          return;
        }
      }

      if (this.timeContent !== '' && this.timeContent !== undefined && this.timeContent !== null) {
        res.content.filter = { shop_order_form_create_time_true_query: this.timeContent };
      } else if (this.startDate !== '' && this.startDate !== undefined && this.startDate !== null) {
        if (this.endDate !== '' && this.endDate !== undefined && this.endDate !== null) {
          // tslint:disable-next-line: max-line-length
          res.content.filter = { shop_order_form_create_time_true_query: `${this.startDate} 00:00:00,${this.endDate} 23:59:59` };
        }
      }
      if (this.shopContent !== '' && this.shopContent !== undefined && this.shopContent !== null) {
        if (res.content.filter) {
          res.content.filter = Object.assign(res.content.filter, { shop_order_form_sales_type_true_query: this.shopContent });
        } else {
          res.content.filter = { shop_order_form_sales_type_true_query: this.shopContent };
        }
      }
      const listenerName = this.tagData.name + "AskForList";
      this.eventService.eventEmit.emit(listenerName, res.content);
      const timer =  setTimeout(() => {
        this.modalCtrl.dismiss();
        clearTimeout(timer);
      }, 50);
    });
  }
  // tslint:disable-next-line: use-life-cycle-interface
  ngAfterViewInit(): void {
    $('#listItem-start').removeClass('am-list-item');
    $('#listItem-end').removeClass('am-list-item');
  }
  itemIsShow(data, item, tag, i, dropDownList) {
    if (item.checked === '1') {
      data.forEach(e => {
        e.checked = 0;
      });
      if (dropDownList.name === 'shop_order_form_create_time') {
        this.timeContent = '';
      } else if (dropDownList.name === 'shop_order_form_sales_type') {
        this.shopContent = '';
      }
    } else {
      data.forEach(e => {
        e.checked = 0;
      });
      item.checked = '1';
      if (dropDownList.name === 'shop_order_form_create_time') {
        data.forEach(e => {
          e.checked = 0;
        });
        item.checked = '1';
        this.timeContent = item.value;
      }
      if (dropDownList.name === 'shop_order_form_sales_type') {
        data.forEach(e => {
          e.checked = 0;
        });
        item.checked = '1';
        this.shopContent = item.value;
      }
      if (tag[i].subFilterItem) {
        tag[i].subFilterItem.forEach(e => {
          // 取消时间范围筛选高亮
          if (e.checked !== undefined && e.name === "startTime") {
            e.checked = '0';
            e.title = '开始时间';
            this.startDate = '';
          }
          if (e.checked !== undefined && e.name === "endTime") {
            if (this.minDate !== undefined) {
              item.startDate = this.minDate;
            }
            e.checked = '0';
            e.title = '结束时间';
            this.endDate = '';
          }
        });
      }
    }
  }
  getDate(max, item, status) {
    if (item.endDate === max) {
      return item.maxDate = item.maxDate ? item.maxDate : this.getDateTime(max);
    } else if (item.startDate === max) {
      if (this.stratTitle !== undefined && item.name === 'endTime' && status === 1) {
        // 如果已选择开始时间
        return item.minDate = this.getDateTime(this.stratTitle);
      } else {
        return item.minDate = item.minDate ? item.minDate : this.getDateTime(max);
      }
    }
  }
  getDateTime(date) {
    if (!date) {
      return;
    } else if ((typeof date !== "string")) {
      return date;
    }
    if (date.indexOf(' ') > -1) {
      return new Date(Date.parse(date.replace(/-/g, "/")));
    } else {
      return new Date(Date.parse((date + ' ' + '23:59:59').replace(/-/g, "/")));
    }
  }
  selectTime(event, item, status, data, subFilterItem) {
    // 保存初始值
    if (this.minDate === undefined || this.minDate === null) {
      this.minDate = item.startDate;
    }
    // 0表示开始时间  1表示结束时间
    if (status === 0) {
      // 如果是无效时间
      if (!this.isValidDate(event)) {
        event = new Date(item.startDate.replace(/\-/g, '/'));
      }
      this.flag = true;
      const date = this.currentDateFormat(event, 'yyyy-mm-dd');

      //  如果选择的开始时间 大于 结束时间    date1开始  date2结束
      subFilterItem.forEach(e => {
        if (e.name === 'endTime' && e.checked === '1') {
          const date1 = date.replace(/\-/g, '/');
          const date2 = e.title.replace(/\-/g, '/');
          if (Date.parse(date1) > Date.parse(date2)) {
            this.appGlobalService.showToast('请重新选择起始时间', 1500, "top");
            this.flag = false;
          }
        }
      });
      if (this.flag === true) {
        item.title = date;
        this.stratTitle = date;
        this.startDate = date;
        item.checked = '1';
      }
    } else if (status === 1) {
      item.title = this.currentDateFormat(event, 'yyyy-mm-dd');
      this.endDate = this.currentDateFormat(event, 'yyyy-mm-dd');
      item.checked = '1';
    }
    // 取消单选按钮高亮
    data.forEach(e => {
      e.checked = '0';
    });
    this.timeContent = '';
  }
  currentDateFormat(date, format: string = 'HH:MM'): any {
    const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    return format
      .replace('yyyy', date.getFullYear())
      .replace('mm', pad(date.getMonth() + 1))
      .replace('dd', pad(date.getDate()));
  }
  isValidDate(date) {
    return date instanceof Date && !isNaN(date.getTime());
  }

}
